import dataTables from 'plugins/datatables.net/js/jquery.dataTables.min.js';

import tableStyles from 'plugins/datatables.net-bs/css/dataTables.bootstrap.css';
import tableStylesJs from 'plugins/datatables.net-bs/js/dataTables.bootstrap.min.js';


$(document).on('hidden.bs.modal', function() {
	$("body").css('padding-right', '0px');
});

$.extend($.fn.dataTable.defaults, {
	"language": {
		"processing": "正在加载中...",
		"lengthMenu": "每页显示 _MENU_ 条记录",
		"zeroRecords": "无数据显示",
		"info": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
		"infoEmpty": "暂无数据",
		"infoFiltered": "(从 _MAX_ 条数据中检索)",
		"search": "搜索",
		"paginate": {
			"first": "首页",
			"previous": "前一页",
			"next": "后一页",
			"last": "最后一页"
		},
		//"emptyTable": "暂无数据"
		"emptyTable": ""
	},
	"order": [
		[0, "desc"]
	],
	"autowidth": true,
	"paging": true, //分页
	"pagingType": "full_numbers", //首页、末页按钮
	"ordering": false, //排序
	"serverSide": true, //服务器模式
	"processing": true, //加载中提示
	"lengthChange": false, //是否允许用户改变表格每页显示的记录数
	"filter": false, //从结果中过滤
	"searching": false, //关闭自带搜索框
	"info": true,
	"fnDrawCallback": function(settings) {
		var count = Math.ceil(settings._iRecordsTotal / settings._iDisplayLength);
		if (count <= 1) {
			$('.dataTables_paginate').hide();
		} else {
			$('.dataTables_paginate').show();
		}
	}
});

//时间格式化
Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month
		"d+": this.getDate(), //day
		"h+": this.getHours(), //hour
		"m+": this.getMinutes(), //minute
		"s+": this.getSeconds(), //second
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
		"S": this.getMilliseconds() //millisecond
	}

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}

	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

import 'plugins/jquery-validation';

// 只验证固定电话
$.validator.addMethod("isTel", function(value, element) {
	var length = value.length;
	var phone =/^(0\d{2}-\d{8}|0\d{3}-\d{7})+(-\d{1,4})?$/;
	var tel = /\d{3,4}-\d{3,4}-\d{3,4}/

    return this.optional(element) || tel.test(value) || phone.test(value);
}, "请正确填写电话");

// 邮政编码验证
$.validator.addMethod("isZipCode", function(value, element) {
	var zip = /^[0-9]{6}$/;
	return this.optional(element) || (zip.test(value));
}, "请正确填写您的邮政编码");

// 版本号验证
$.validator.addMethod("version", function(value, element) {
	var version = /^[0-9]*(\.[0-9]*){0,2}$/;
	// var version = /^[0-9]\.[0-9]\.[0-9]$/;
    return this.optional(element) || (version.test(value));
}, "请输入正确的版本号（如：1.0.0）");

//是否整数
$.validator.addMethod("int", function(value, element) {
	if ((parseInt(value) == value)) {
		element.value = parseInt(value);
	}
	return (parseInt(value) == value) ? true : false;
},"请输入整数");

//数字或字母
$.validator.addMethod("isCode", function(value, element) {
	var zip = /^[0-9a-zA-Z]*$/;
	return this.optional(element) || (zip.test(value));
}, "请正确填写数字或字母");

//只验证手机
jQuery.validator.addMethod("mobile", function(value, element) {
	var length = value.length;
	var mobile = /^1[3|4|5|7|8]\d{9}$/;
	return this.optional(element) || mobile.test(value);
}, "手机格式错误");

//手机和电话都可以验证
jQuery.validator.addMethod("isPhone", function(value, element) {
	var length = value.length;
	// var phone =  /^((^(0\d{2}-\d{8}|0\d{3}-\d{7,8})+(-\d{1,4})?$)|(\d{3,4}-\d{3,4}-\d{3,4})|(1[35784]\d{9}))$/;
    var phone =  /^((^(0\d{2}-|\d{8}|0\d{3}-|\d{7,8})+(-|\d{1,4})?$)|(\d{3,4}-\d{3,4}-\d{3,4})|(1[35784]\d{9}))$/;
	return this.optional(element) || (phone.test(value));
}, "请正确填写您的联系电话");

//验证小数点后保留两位
jQuery.validator.addMethod("decimal", function(value, element) {
	return this.optional(element) || /^[0-9]+([.]\d{1,2})?$/.test(value);
}, "只能保留两位小数");

jQuery.validator.addMethod("decimal2", function(value, element) {
    return this.optional(element) || /^[0-9]{0,8}([.]\d{0,2})?$/.test(value);
}, "整数最大八位,精度最多两位");

//只能包含中文、英文、数字、下划线等字符
jQuery.validator.addMethod("stringCheck", function(value, element) {
	return this.optional(element) || /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);
}, "只能包含中文、英文、数字、下划线等字符");

//字母数字
jQuery.validator.addMethod("alnum", function(value, element) {
	return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, "只能包括英文字母和数字");

// 字符最大长度验证（一个中文字符长度为2）
jQuery.validator.addMethod("stringMaxLength", function(value, element, param) {
	var length = value.length;
	for (var i = 0; i < value.length; i++) {
		if (value.charCodeAt(i) > 127) {
			length++;
		}
	}
	return this.optional(element) || (length <= param);
}, $.validator.format("最多可以输入{0}个字符（一个中文字为2个字符）"));

//日期比较
jQuery.validator.addMethod("compareDate", function(value, element, param) {
	let startDate = document.getElementsByName(param[0])[0].value.replace(/-/g, "/")
	let endDate = document.getElementsByName(param[1])[0].value.replace(/-/g, "/")

	if (!startDate) {
		return true
	}
	if (!endDate) {
		return true
	}
	if (Date.parse(startDate) <= Date.parse(endDate)) {
		if (element == document.getElementsByName(param[0])[0]) {
			$('[name=' + param[1] + ']+label.error').remove()
		} else {
			$('[name=' + param[0] + ']+label.error').remove()
		}
	}
	return this.optional(element) || (Date.parse(startDate) <= Date.parse(endDate));
}, "开始时间不能大于结束时间");

jQuery.validator.addMethod("compareDateAno", function(value, element, param) {
	// debugger
	let startDate = document.getElementsByName(param[0])[0].value;
	let endDate = document.getElementsByName(param[1])[0].value;

	if (!startDate) {
		return true
	}
	if (!endDate) {
		return true
	}
	return this.optional(element) ||  startDate <= endDate;
}, "开始时间不能大于或等于结束时间");
//日期比较
jQuery.validator.addMethod("compareTime", function(value, element, param) {
	let startDate = document.getElementsByName(param[0])[0].value.replace(/-/g, "/");
	let endDate = document.getElementsByName(param[1])[0].value.replace(/-/g, "/");
	if (/^\d{2}[:]\d{2}[:]\d{2}$/.test(value)) {
		startDate = "2016/08/01 " + startDate;
		endDate = "2016/08/01 " + endDate;
	}
	if (!startDate) {
		return true
	}
	if (!endDate) {
		return true
	}
	if (Date.parse(startDate) <= Date.parse(endDate)) {
		if (element == document.getElementsByName(param[0])[0]) {
			$('[name=' + param[1] + ']+label.error').remove()
		} else {
			$('[name=' + param[0] + ']+label.error').remove()
		}
	}
	return this.optional(element) || (Date.parse(startDate) <= Date.parse(endDate));
}, "开始时间不能大于结束时间");

//比较数字大小
jQuery.validator.addMethod("comparisonSize", function(value, element, param) {
	let startDate = parseInt(document.getElementsByName(param[0])[0].value);
	let endDate = parseInt(document.getElementsByName(param[1])[0].value);
	if (startDate <= endDate || (startDate == ''&&endDate == '')) {
		if (element == document.getElementsByName(param[0])[0]) {
            $('[name=' + param[1] + ']+label.error').remove()
		} else {
			$('[name=' + param[0] + ']+label.error').remove()
		}
	}
	if (startDate == 0) {
		return true
	}

	return this.optional(element) || (startDate < endDate);
}, "开始时间不能大于等于结束时间");

jQuery.validator.addMethod("comparisonBeginDate", function(value, element, param) {
	let startDate = new Date(document.getElementsByName(param[0])[0].value).getTime();
	let endDate = new Date(document.getElementsByName(param[1])[0].value).getTime();
	let isTrue
	if (endDate) {
		isTrue = (startDate <= endDate)
	} else {
		isTrue = true
	}
	return this.optional(element) || isTrue;
}, "开始时间不能大于结束时间");

jQuery.validator.addMethod("comparisonDate", function(value, element, param) {
	let startDate = new Date(document.getElementsByName(param[0])[0].value).getTime();
	let endDate = new Date(document.getElementsByName(param[1])[0].value).getTime();
	let isTrue
	if (startDate) {
		isTrue = (startDate <= endDate)
	} else {
		isTrue = true
	}
	return this.optional(element) || isTrue;
}, "结束时间不能小于开始时间");

jQuery.validator.addMethod("money", function(value, element, param) {
	// let money = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/
	let money =/^(-?\d+)(\.\d+)?$/
	return this.optional(element) || (money.test(value));
}, "金额不能为负数,并且只能保留两位小数");

jQuery.validator.addMethod("intMoney", function(value, element, param) {
	let monVal = /^(0|[1-9][0-9]*)?$/
	return this.optional(element) || monVal.test(value);
}, "金额必须为正整数");

jQuery.validator.addMethod("reviewLvl", function(value ) {
	return value > 0 && value < 100;
}, "审核等级必须为大于0小于100的整数");

jQuery.validator.addMethod("num", function(value ) {
	return value > 0 && value < 1;
}, "比例必须是大于0小于1的小数");

jQuery.validator.addMethod("cash", function(value ) {
    return value > 0;
}, "金额必须大于0");
//验证身份证号
jQuery.validator.addMethod("isIDCard", function(value, element) {
	var length = value.length;
	var identity =/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
	return this.optional(element) || identity.test(value);
}, "请正确填写您的身份证号");

//验证座机和手机号
jQuery.validator.addMethod("phone", function(value, element) {
	var length = value.length;
	var pattern =/(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)/;
	return this.optional(element) || pattern.test(value);
}, "请正确填写您的号码");

//验证0~1之间的两位小数
jQuery.validator.addMethod("decNum", function(value, element) {
    return this.optional(element) || /^0\.(0[1-9]|[1-9]{1,2})$/.test(value);
}, "只能是0~1之间的两位小数");

//验证只能是数字 和 逗号
jQuery.validator.addMethod("rule", function(value, element) {
    return this.optional(element) || /^[\d\,]*$/.test(value);
}, "只能输入正整数和英文逗号");
